package com.mainService.newService;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.dubboInterface.merchant.crawleServiceInterface.shanxiInterface.ShanxiBaseClick;
import com.mainService.mapper.SmokeMapper;
import com.mainService.service.SmokeService;
import com.mainService.temp.TempSmokeListBean;
import com.projectCommon.bean.ProductBeanResult;
import com.projectCommon.bean.Smoke;
import com.projectCommon.bean.httpSmokeBean.shanxitobaccobean.response.SmokeResult;
import com.projectCommon.util.commonUtil.FileUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// 临时的一个数据
@SpringBootTest
public class DownList {
    @Reference
    private ShanxiBaseClick shanxiBaseClick;
    @Autowired
    private SmokeService smokeServiceImpl;
    @Autowired
    private SmokeMapper smokeMapper;

    @Test
    public void downList() {
        InputStream fileInputStream;
        FileOutputStream outputStream = null;
        try {
//            fileInputStream = DownList.class.getClassLoader().getResourceAsStream("template/list.xls");
            fileInputStream = Files.newInputStream(new File("D:\\gitee_versionControllerProject\\project-merchant-management\\mavenConformity\\mainService\\src\\main\\resources\\template\\list.xls").toPath());
            outputStream = new FileOutputStream(new File("C:\\Users\\wy\\Desktop\\2222222\\1.xls"));
            SmokeResult smokeResult = shanxiBaseClick.selectAllSmokeInfo();
            List<ProductBeanResult> rows = smokeResult.getRows();
            List<TempSmokeListBean> tempSmokeListBeans = new ArrayList<>();
            for (ProductBeanResult productBeanResult : rows) {
                TempSmokeListBean tempSmokeListBean = new TempSmokeListBean();
                String org_is_abnormity = productBeanResult.getOrg_is_abnormity();
                if (org_is_abnormity.equals("1")){
                    org_is_abnormity = "是";
                }else if (org_is_abnormity.equals("0")){
                    org_is_abnormity = "否";
                }
                tempSmokeListBean.setIsAbnormity(org_is_abnormity);
                tempSmokeListBean.setAdvicePrice(productBeanResult.getRetail_price());
                tempSmokeListBean.setName(productBeanResult.getProduct_name());
                tempSmokeListBean.setWholesalePrice(productBeanResult.getWhole_sale_price());
                tempSmokeListBean.setProductUUID(productBeanResult.getProduct_uuid());
                tempSmokeListBeans.add(tempSmokeListBean);
            }
            List<Smoke> smokes = smokeMapper.selectAllLatestSmoke();
            Map<String, Smoke> collect = smokes.stream().collect(Collectors.toMap(Smoke::getProductUuid, smoke -> smoke));
            for (TempSmokeListBean tempSmokeListBean : tempSmokeListBeans) {
                String productUUID = tempSmokeListBean.getProductUUID();
                Smoke smoke = collect.get(productUUID);
                if (smoke == null) {
                    continue;
                }
                String realSalePriceBar = smoke.getRealSalePriceBar();
                String realSalePriceBox = smoke.getRealSalePriceBox();
                if (realSalePriceBar != null) {
                    tempSmokeListBean.setActualSellPrice_T(realSalePriceBar);
                }
                if (realSalePriceBox != null) {
                    tempSmokeListBean.setActualSellPrice_H(realSalePriceBox);
                }
            }
            tempSmokeListBeans.sort((o1, o2) -> {
                String wholesalePrice = o1.getWholesalePrice();
                String wholesalePrice1 = o2.getWholesalePrice();
                double v1 = Double.parseDouble(wholesalePrice);
                double v2 = Double.parseDouble(wholesalePrice1);
                return (int) (v1 - v2);
            });
            for (int i = 0; i < tempSmokeListBeans.size(); i++) {
                TempSmokeListBean tempSmokeListBean = tempSmokeListBeans.get(i);
                tempSmokeListBean.setId(i+1 + "");
            }
            WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
            ExcelWriter excelWriterBuild = EasyExcel
                    .write(outputStream)
                    .withTemplate(fileInputStream)
                    .excelType(ExcelTypeEnum.XLS)
                    .build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriterBuild.fill(new FillWrapper("list", tempSmokeListBeans), fillConfig, writeSheet);
            excelWriterBuild.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}